//
//  LCCouponListViewController.m
//  lechuang
//
//  Created by Mr_hi on 2023/3/27.
//  Copyright © 2023 Mooyi. All rights reserved.
//

#import "LCCouponListViewController.h"
#import "LCCouponCollectionHeaderView.h"
#import "LCGouponCollectionViewCell.h"

#import "LCCouponService.h"
#import "XWXAllXieYiController.h"

@interface LCCouponListViewController ()<UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (assign, nonatomic)NSInteger page;
@property (strong, nonatomic)NSMutableArray * dataArr;

@property (nonatomic, assign) BOOL canScroll;

@property (strong, nonatomic)LCTaoKeFavoritesModel *favorites;

@end

@implementation LCCouponListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    [self setContentViewRefresh];
    [self getSoureData];
}

-(void)configUI {
    self.page = 1;
    self.canScroll = NO;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(couponListControllerGoTop:) name:LCCouponListControllerGoTopNoti object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(couponListControllerLeaveTop:) name:LCCouponListControllerLeaveTopNoti object:nil];
}

-(void)couponListControllerGoTop:(NSNotification *)noti {
    self.canScroll = YES;
    CGFloat height = self.collectionView.frame.size.height;
    if (self.collectionView.contentSize.height < height) {
        self.canScroll = NO;
        [[NSNotificationCenter defaultCenter] postNotificationName:LCCouponListControllerLeaveTopNoti object:@(YES)];
    }
}

-(void)couponListControllerLeaveTop:(NSNotification *)noti {
    self.canScroll = NO;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (self.canScroll) {
        CGFloat offsetY = scrollView.contentOffset.y;
        if (offsetY <= 0) {
            [self.collectionView setContentOffset:CGPointZero];
            [[NSNotificationCenter defaultCenter] postNotificationName:LCCouponListControllerLeaveTopNoti object:@(YES)];
        }
    } else {
        [self.collectionView setContentOffset:CGPointZero];
    }
}

- (void)getSoureData {
    if (!self.favorites) {
        [self getTaoKeFavorites];
    } else {
        [self getTaoKeGoodsData];
    }
}

- (void)getTaoKeFavorites {
    [LCCouponService getTaoKeFavoritesListWithBlock:^(BOOL isSuccess, NSArray<LCTaoKeFavoritesModel *> *list) {
        if (isSuccess) {
            self.favorites = [list firstObject];
            [self getTaoKeGoodsData];
        }
    }];
}

- (void)getTaoKeGoodsData {
    [LCCouponService getTaoKeGoodsListWithPage:self.page FavoritesId:kFORMAT(@"%@",self.favorites.favorites_id) Block:^(BOOL isSuccess, NSArray<LCTaoKeGoodsModel *> *list, NSInteger total) {
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView.mj_footer endRefreshing];
        if (isSuccess) {
            if (self.page == 1) {
                [self.dataArr removeAllObjects];
            }
            [self.dataArr addObjectsFromArray:list];
            if (self.dataArr.count >= total) {
                [self.collectionView.mj_footer endRefreshingWithNoMoreData];
            }
            [self.collectionView reloadData];
        } else {
            if (self.page != 1) {
                self.page--;
            }
        }
    }];
}


-(NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

-(void)setContentViewRefresh {
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page++;
        [self getSoureData];
    }];
    self.collectionView.ly_emptyView = [LCEmptyView emptyViewWithImage:nil titleStr:@"暂无数据～" detailStr:nil];
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(14, 17, 14, 17);
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 12;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 12;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArr.count;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat cellW = (ScreenW-46)/2;
    CGFloat cellH = cellW+97;
    return CGSizeMake(cellW, cellH);
}

-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LCGouponCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LCGouponCollectionViewCell" forIndexPath:indexPath];
    cell.item = self.dataArr[indexPath.item];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    LCTaoKeGoodsModel * item = self.dataArr[indexPath.item];
    XWXAllXieYiController *webVC = [XWXAllXieYiController new];
    webVC.tileStr = @"商品详情";
    webVC.urlStr = kFORMAT(@"http:%@",item.click_url);
    if (item.coupon_share_url.length) {
        webVC.urlStr = kFORMAT(@"http:%@",item.coupon_share_url);
    }
    webVC.isPushFrom = YES;
    webVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:webVC animated:YES];
}

@end
